package com.example.express_order.dao;

import com.example.express_order.domain.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderDao {
    @Insert("INSERT INTO `order`(order_id,user_phone,express_detail_address,delivery_address,delivery_mode,express_type,estimated_price,user_leisure,stuff_detail) VALUES(#{orderId},#{userPhone},#{expressDetailAddress},#{deliveryAddress},#{deliveryMode},#{expressType},#{estimatedPrice},#{userLeisure},#{stuffDetail})")
    Boolean addOrder(Order order);

    /**
     * 通过快递点查找订单,订单状态为为0，GPS定位，搜索框搜索
     * @param expressDetailAddress
     * @return
     */
    @Select("SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\t`order` \n" +
            "WHERE\n" +
            "\texpress_detail_address LIKE concat('%',#{expressDetailAddress},'%') and order_state=0")
    List<Order> findOrdersByExpressDetailAddress(String expressDetailAddress);


    //通过收货地址搜索订单
    @Select("SELECT * FROM `order` WHERE delivery_address LIKE concat('%',#{deliveryAddress},'%') and order_state=0")
    List<Order> findOrdersByDeliveryAddress(String deliveryAddress);

    //通过订单类型和快递详细地址搜索订单
    @Select("SELECT * FROM `order` WHERE express_detail_address LIKE concat('%',#{expressDetailAddress},'%') and order_state=0 and express_type=#{expressType}")
    List<Order> findOrderByExpressType(String expressDetailAddress, String expressType);


    //快递员接单，修改订单状态为1（以接单），添加快递员信息和接单时间
    @Update("UPDATE `order` SET order_state=#{orderState},courier_phone=#{courierPhone},receive_order_time=#{receiveOrderTime} WHERE order_id=#{orderId}")
    Boolean updateOrderStatusAndCourierByOrderID(int orderState, String courierPhone, String orderId, String receiveOrderTime);

    /**
     *
     * 客户订单完成，修改订单状态为2（完成）
     *
     */
    @Update("UPDATE `order` SET order_state=#{orderState},order_finish_time=#{orderFinishTime} WHERE order_id=#{orderId}")
    Boolean modifyOrderStatusByOrderId(int orderState, String orderId, String orderFinishTime);

    /**
     * 管理员订单审核
     * @param orderState
     * @param orderId
     * @return
     */
    @Update("UPDATE `order` SET order_state=#{orderState} WHERE order_id=#{orderId}")
    Boolean adminCheckOrder(int orderState, String orderId);
    //普通用户历史订单查看
    @Select("SELECT * FROM `order` WHERE user_phone=#{userPhone} AND order_state=2")
    List<Order> findHistoryCommonUserOrderByPhone(String userPhone);
    //配送历史查看，快递员可以查看自己以配送的订单
    @Select("SELECT * FROM `order` WHERE courier_phone=#{courierPhone} AND order_state=2")
    List<Order> findHistoryDeliveryOrderByCourierPhone(String courierPhone);
    //订单管理员，审核订单，订单违规，则标注为3
    @Select("SELECT * FROM `order` WHERE order_state=0")
    List<Order> findOrderOfStatusEqualZero();

    //查看刚发布的订单
    @Select("SELECT * FROM `order` WHERE order_state=0 and user_phone=#{userPhone}")
    List<Order> findMyPostOrder(String userPhone);
    //查看已被接单还未完成的订单
    @Select("SELECT * FROM `order` WHERE order_state=1 and user_phone=#{userPhone}")
    List<Order> findWaitFinishOrders(String userPhone);
    //查看我的接单
    @Select("SELECT * FROM `order` WHERE order_state=1 and courier_phone=#{courierPhone}")
    List<Order> findMyReceiveOrders(String courierPhone);

}
